Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(e2e): add nominal tests for exec & init CLI commands #434

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ErwanRaulo
Copy link
Contributor

@ErwanRaulo ErwanRaulo commented Dec 31, 2024

TOFIX: could run tests before TS migration, now something goes wrong with childprocess and execution path.

when running nreport init, main process throws the same error :
Error: ENOENT: no such file or directory, open 'C:<yourDevDir>\report\dist\views\template.html'

process try to access views from dist folder but it does not exists, should be added to dist folder as asset in tsconfig file to include a copy when compiling typescript

@ErwanRaulo ErwanRaulo force-pushed the test-cli-integration branch 2 times, most recently from 9425a2f to bae3766 Compare December 31, 2024 23:46
@@ -19,6 +19,7 @@
"lint": "eslint src test bin scripts",
"test-only": "glob -c \"tsx --test-reporter=spec --test\" \"./test/**/*.spec.ts\"",
"test": "c8 --all --src ./src -r html npm run test-only",
"test:e2e": "glob -c \"tsx --test-reporter=spec --test\" \"./test/commands/*.spec.ts\"",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think functional tests are not executed during development as we expect quick feedback from small unit tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to have a separate script for e2e tests, however, the glob from existing test-only script still match e2e tests files.
I think it's simpler to handle this via file names, for instance:

{
  "test-only": "glob -c \"tsx --test-reporter=spec --test\" \"./test/**/*.spec.ts\"",
  "test:e2e": "glob -c \"tsx --test-reporter=spec --test\" \"./test/**/*.e2e-spec.ts\""
}

}

export function filterProcessStdout(options, filter): Promise<string[]> {
return new Promise((resolve, reject) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

childprocess was turned into promise in order to collect filtered stdout outputs and emit them all at the end.

Comment on lines 65 to 66
let actualLines: string[] = [];

try {
actualLines = await filterProcessStdout(options, byMessage);
}
catch (error) {
console.log(error);

assert.fail("Execute command should not throw");
}

assert.deepEqual(actualLines, expectedLines, "we are expecting these lines");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let actualLines: string[] = [];
try {
actualLines = await filterProcessStdout(options, byMessage);
}
catch (error) {
console.log(error);
assert.fail("Execute command should not throw");
}
assert.deepEqual(actualLines, expectedLines, "we are expecting these lines");
const actualLines = await filterProcessStdout(options, byMessage);
assert.deepEqual(actualLines, expectedLines, "we are expecting these lines");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pas forcément besoin de rendre complexe juste pour un message de failure spécifique

Comment on lines +18 to +23
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const processDir = path.join(__dirname, "..", "fixtures");
const configFilePath = path.join(processDir, ".nodesecurerc");

describe("Report init command", async() => {
beforeEach(async() => await fs.unlink(configFilePath));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Il faudrait plutôt créer la configuration dans le path temp pour ne pas avoir de problèmes avec une modification non-prévu du fichier dans /fixtures

@PierreDemailly
Copy link
Member

Hi @ErwanRaulo

when running nreport init, main process throws the same error :
Error: ENOENT: no such file or directory, open 'C:\report\dist\views\template.html'

I don't have this problem, are you sure you built the project first ?
The "rimraf dist/views && cp -r views dist/views" script should work 🤔
image

@ErwanRaulo ErwanRaulo force-pushed the test-cli-integration branch from bae3766 to 1c7450a Compare January 19, 2025 17:24
no breaking changes
use childprocess to collect CLI stdout
missing limit case tests
fix NodeSecure#395
@@ -19,6 +19,7 @@
"lint": "eslint src test bin scripts",
"test-only": "glob -c \"tsx --test-reporter=spec --test\" \"./test/**/*.spec.ts\"",
"test": "c8 --all --src ./src -r html npm run test-only",
"test:e2e": "glob -c \"tsx --test-reporter=spec --test\" \"./test/commands/*.spec.ts\"",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to have a separate script for e2e tests, however, the glob from existing test-only script still match e2e tests files.
I think it's simpler to handle this via file names, for instance:

{
  "test-only": "glob -c \"tsx --test-reporter=spec --test\" \"./test/**/*.spec.ts\"",
  "test:e2e": "glob -c \"tsx --test-reporter=spec --test\" \"./test/**/*.e2e-spec.ts\""
}

Comment on lines +1 to +2
import dotenv from "dotenv";
dotenv.config();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import dotenv from "dotenv";
dotenv.config();

Require dotenv in the script instead ? This way we don't need to import it in multiple files
"test:e2e": "glob -c \"tsx -r dotenv/config --test-reporter=spec --test\" \"./test/commands/*.spec.ts\""

Copy link
Member

@PierreDemailly PierreDemailly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit picks, this is good job 💪

test/commands/execute.spec.ts Show resolved Hide resolved
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const processDir = path.join(__dirname, "../..");

describe("Report execute command", async() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
describe("Report execute command", async() => {
describe("Report execute command", () => {

}

const expectedLines = [
"Executing nreport at: C:\\PERSO\\dev\\report",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Executing nreport at: C:\\PERSO\\dev\\report",
`Executing nreport at: ${kProcessDir}`,

Comment on lines +1 to +3
import dotenv from "dotenv";
dotenv.config();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import dotenv from "dotenv";
dotenv.config();

Same as above


// CONSTANTS
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const processDir = path.join(__dirname, "..", "fixtures");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const processDir = path.join(__dirname, "..", "fixtures");
const kProcessDir = path.join(__dirname, "..", "fixtures");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why it's prefixed by k ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a convention for all the constants in all our projects: file-scoped constants should be prefixed with k, exported constants should be named using CONSTANT_CASE/UPPER_CASE, and other constants should be named using camelCase :)

AFAIK this convention comes from the Google C++ style guide used in Node.js (it's more used for Symbols tho)
Examples:
https://github.com/nodejs/node/blob/main/lib/events.js#L62
https://github.com/nodejs/node/blob/main/lib/events.js#L87
Another example at NodeSecure:
https://github.com/NodeSecure/cli/blob/master/src/http-server/cache.js#L13

// CONSTANTS
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const processDir = path.join(__dirname, "..", "fixtures");
const configFilePath = path.join(processDir, ".nodesecurerc");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const configFilePath = path.join(processDir, ".nodesecurerc");
const kConfigFilePath = path.join(processDir, ".nodesecurerc");

const processDir = path.join(__dirname, "..", "fixtures");
const configFilePath = path.join(processDir, ".nodesecurerc");

describe("Report init command", async() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
describe("Report init command", async() => {
describe("Report init command", () => {

@ErwanRaulo
Copy link
Contributor Author

Thanks for feedback, I will correct them all soon.

And yes I can run " nreport init " now, don't known if it was thanks to the latest commit or my fault but in my TS migration PR I've used rimraf also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants